<template>
  <el-dialog title="开发票" :visible.sync="showKfp" @close="cancelKfp" width="50%" append-to-body>
    <el-form ref="kpform" :model="form" :rules="rulesFp" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="发票票号" prop="fpph">
            <el-input v-model="form.fpph" placeholder="请输入发票票号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="开票日期" prop="kprq">
            <el-date-picker clearable
                            style="width: 100%"
                            v-model="form.kprq"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择开票日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="kaiFp">确 定</el-button>
      <el-button @click="cancelKfp">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateWgd } from '@/api/gd/wgd'

export default {
  name: 'kfpForm',
  props: {
    showKfp: {
      type: Boolean
    },
    formInfo: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      buttonLoading: false,
      // 表单校验
      rulesFp: {
        fpph: [
          { required: true, message: "发票票号不能为空", trigger: "blur" }
        ],
      },
    }
  },
  created() {
    this.form = this.formInfo
  },
  methods: {
    cancelKfp() {
      this.$emit("close")
    },
    kaiFp() {
      // 判断当前的开票状态，如果未开票
      if (this.form.sfykfp === 'N') {
        this.$refs["kpform"].validate(valid => {
          if (valid) {
            // 如果是未开票状态则将状态改为已开票
            this.$set(this.form, 'sfykfp', 'Y')
            this.form.status = '0'
            updateWgd(this.form).then(response => {
              this.$modal.msgSuccess("开票成功！");
              this.cancelKfp()
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        })
      }
    },
  }
}
</script>

<style scoped>

</style>
